<?php

namespace apps\marriage\app\mp\controller;

use apps\marriage\core\service\MarketingActivityService;
use think\Exception;
use think\Request;

class MarketingActivityController extends BaseMarriageMpController
{
    /**
     * @var MarketingActivityService
     */
    private $marketingActivityService;

    protected function init()
    {
        parent::init();
        $this->marketingActivityService = service('MarketingActivity', SERVICE_NAMESPACE);
    }

    public function info(Request $request)
    {
        $id = $request->param('id', 0, 'intval');
        $activity = $this->marketingActivityService->getByPk($id);
        if(empty($activity) || $activity['enable'] == 0) {
            throw new Exception('活动不存在');
        }
        $activity = $this->marketingActivityService->format($activity);
        return $this->success(['activity' => $activity]);
    }

    public function join(Request $request)
    {
        $activityId = $request->param('activity_id', 0, 'intval');
        $activity = $this->marketingActivityService->getByPk($activityId);
        if(empty($activity)) {
            throw new Exception('活动不存在');
        }
        if($activity['enable'] == 0) {
            throw new Exception('活动已下线');
        }
        $this->marketingActivityService->onJoin($this->user['id'], $activityId);
        return $this->success();
    }
}